<?php
namespace Admin\Controller;
use Think\Controller;
use Myself\FileSystem;
use Admin\Controller\CommonController;
class FinanceController extends CommonController { 

	protected $statusArr=array("下架","上架");
	protected $isHome=array("未推荐","已推荐");
	
	public   function select_class(){
	
	$list_one_id = I('checkValueOne');
	if( $list_one_id != ' '){
	  $list_two = M('shop_product_class')->where('pid="'.$list_one_id.'"')->select();
	  $list_str.=' <option selected="selected" value="">'.'请选择'.'</option>';
	
		foreach($list_two as $key=>$item){
		   $list_str.=' <option  value='.$item['id'].'>'.$item['class_name'].'</option>';
		}
	   } else{
		 $list_str.=' <option selected="selected" value="">'.'请选择'.'</option>';
		}
		
		 $this->ajaxReturn( $list_str );
	
	}

public   function select_address(){
	
	$list_one_id = I('checkValueOne');
	if( $list_one_id != ' '){
	  $list_two = M('shop_address')->where('pid="'.$list_one_id.'"')->select();
	  $list_str.=' <option selected="selected" value="">'.'请选择'.'</option>';
	
		foreach($list_two as $key=>$item){
		   $list_str.=' <option  value='.$item['id'].'>'.$item['name'].'</option>';
		}
	   } else{
		 $list_str.=' <option selected="selected" value="">'.'请选择'.'</option>';
		}
		
		 $this->ajaxReturn( $list_str );
	
	}

  public function index(){
		header('Content-Type:text/html;charset=utf-8');
		//print_r(I('select_name1'));
		//-----------------搜索-------------------
		
	        $list_one= M('shop_product_class')->where('level=1')->select(); 
	        $list_address= M('shop_address')->where('level=1')->select(); 
	        if(!empty(I('select_name1'))){ 
		   $where['shop_level_one']=I('select_name1');
		}
		if(!empty(I('select_name2'))){
		   $where['shop_level_two']=I('select_name2');
		}
		if(!empty(I('select_name3'))){
		   $where['shop_level_three']=I('select_name3');
		}
		if(!empty(I('select_name4'))){
		   $where['shop_level_four']=I('select_name4');
		}
		if(!empty(I('select_name5'))){
		   $where['shop_prv']=I('select_name5');
		}
		if(!empty(I('select_name6'))){
		   $where['shop_city']=I('select_name6');
		}
		if(!empty(I('select_name7'))){
		   $where['shop_school']=I('select_name7');
		}
		
		$sh_q = I('sh_q','');//关键字
		if(!empty($sh_q)){
			$shArray['sh_q'] = $sh_q;
			$where['title'] = array('LIKE','%'.$sh_q.'%');
		}
		

		$from_time = I('from_time','');
		$to_time = I('to_time','');
		  
		if($from_time){
			$from_time_s = strtotime($from_time);
			$where['addtime']= array('EGT',$from_time_s);
			$shArray['from_time'] = $from_time;
		}
		
		if($to_time){
			$to_time_s = strtotime($to_time)+86400;
			$where['addtime']= array('ELT',$to_time_s);
			$shArray['to_time'] = $to_time;	
		}		
		
		if($from_time && $to_time){
			$where['addtime']=array(array('EGT',$from_time_s),array('ELT',$to_time_s),'and');
		}	
			  
		if(!empty($shArray)){$this->assign('shArray',$shArray);}// 搜索参数输出
		//-------------------------搜索end-----------------

		$pageno = I('p',1);
                $pageCount = 10;
		$list = M('shop')->where($where)->order('id desc')->page($pageno.','.$pageCount)->select();
		foreach($list as $key=>$item){
		        $list[$key]['keeper'] = M('shop_detail')->where('s_id="'.$item['id'].'"')->getField('keeper');
                        $list[$key]['bank_card_keeper'] = M('shop_detail')->where('s_id="'.$item['id'].'"')->getField('bank_card_keeper');
			$list[$key]['idcard'] = M('shop_detail')->where('s_id="'.$item['id'].'"')->getField('idcard');
			$list[$key]['bankcard'] = M('shop_detail')->where('s_id="'.$item['id'].'"')->getField('bankcard');
			$list[$key]['bank'] = M('shop_detail')->where('s_id="'.$item['id'].'"')->getField('bank');
			$list[$key]['add_time'] = M('shop_detail')->where('s_id="'.$item['id'].'"')->getField('build_time');
		        $list[$key]['bank_branch'] = M('shop_detail')->where('s_id="'.$item['id'].'"')->getField('bank_branch');
		        $list[$key]['telephone'] = M('shop_detail')->where('s_id="'.$item['id'].'"')->getField('telephone');
                        $list[$key]['w_money'] =  M('shop_money')->where('mid="'.$item['mid'].'"')->getField('w_money');//待回款金额
                        $list[$key]['h_money'] =  M('shop_money')->where('mid="'.$item['mid'].'"')->getField('h_money');//已回款金额
                        $list[$key]['wt_money'] =  M('shop_money')->where('mid="'.$item['mid'].'"')->sum('w_money');//待回款总金额
                        $list[$key]['ht_money'] =  M('shop_money')->where('mid="'.$item['mid'].'"')->sum('h_money');//已回款总金额 
		        $list[$key]['h_money_status'] =  M('shop_money')->where('mid="'.$item['mid'].'"')->sum('h_money_status');
                        }
	        $this->assign('list',$list);
		$count = M('shop')->where($where)->count();
		$this->assign('count',$count);	
		$Page = new \Think\Page($count,$pageCount);
		$show = $Page->showAdmin();
		$this->assign('page',$show);	
                $this->assign('list1',$list_one);
		$this->assign('listAddress',$list_address);
		$this->assign('statusArr',$this->statusArr);
	        $this->display('Finance/index');
    }
	
	
	
	
	  public  function disable(){
	    $buss_status = I('buss_status');
	    $id = I('id');
		if(!empty( $buss_status ) && !empty( $id )){
		 $shop_id = M('shop')->where('id="'.$id.'"')->getField('shop_id'); 
		   if( $buss_status == 1){
		      $data['buss_status'] = 2;
		      $resultA =  M('shop_detail')->where('shop_id="'.$shop_id.'"')->save($data);
		   } else{
		   
		      $data['buss_status'] = 1;
		      $resultB =  M('shop_detail')->where('shop_id="'.$shop_id.'"')->save($data);
		   
		   }
		   
		   
		   if($resultA || $resultB){
            
			$this->redirect(U('shopMange/index'));

		   }
		}
	
	
	
	  }
	
	public function editPoint(){
		header('Content-Type:text/html;charset=utf-8');
		$id = I('id',0,'int');
		$dataInfo = M('shop')->find($id);
		$dataInfo['keeper'] = M('shop_detail')->where('shop_id="'.$dataInfo['shop_id'].'"')->getField('keeper');
		$dataInfo['phone'] = M('user')->where('user_id="'.$dataInfo['shop_id'].'"')->getField('phone');
		$dataInfo['idcard'] = M('shop_detail')->where('shop_id="'.$dataInfo['shop_id'].'"')->getField('idcard');
		$dataInfo['bankcard'] = M('shop_detail')->where('shop_id="'.$dataInfo['shop_id'].'"')->getField('bankcard');
		$dataInfo['bank'] = M('shop_detail')->where('shop_id="'.$dataInfo['shop_id'].'"')->getField('bank');
		$dataInfo['build_time'] = M('shop_detail')->where('shop_id="'.$dataInfo['shop_id'].'"')->getField('build_time');
		$dataInfo['provin'] = M('shop_detail')->where('shop_id="'.$dataInfo['shop_id'].'"')->getField('provin');
		$dataInfo['city'] = M('shop_detail')->where('shop_id="'.$dataInfo['shop_id'].'"')->getField('city');
		$dataInfo['point'] = M('shop_detail')->where('shop_id="'.$dataInfo['shop_id'].'"')->getField('point');
		$dataInfo['bank_branch'] = M('shop_detail')->where('shop_id="'.$dataInfo['shop_id'].'"')->getField('bank_branch');
		$dataInfo['buss_status'] = M('shop_detail')->where('shop_id="'.$dataInfo['shop_id'].'"')->getField('buss_status');
		$this->assign('dataInfo', $dataInfo);
	
		$this->display('Shop/edit');
    }	
	
	
	 public function checkStatus(){
	     $cid = I('cid');
	     $dataInfo = M('shop')->find($cid);
	     $data['check_status'] = 1;
	     $result = M('shop_detail')->where('shop_id="'.$dataInfo['shop_id'].'"')->save($data);
	     if( $result ){
		 $data = 100;
		 $this->ajaxReturn($data);
		 }
	 
	 }
	 
	 
	   public function savePoint(){
	     $cid = I('cid');
             $data['point'] =  I('kd');
	     $cid = I('cid');
	     $dataInfo = M('shop')->find($cid);
	     $result = M('shop_detail')->where('shop_id="'.$dataInfo['shop_id'].'"')->save($data);
		 if( $result ){
		 $data = 100;
		 $this->ajaxReturn($data);
		 }
	 
	   }
}